/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.imenu.ejb;

import com.imenu.entity.ImenuFavorite;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Yunzi
 */
@Stateless
public class ImenuFavoriteFacade extends AbstractFacade<ImenuFavorite> {

    @PersistenceContext(unitName = "iMenuPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public ImenuFavoriteFacade() {
        super(ImenuFavorite.class);
    }

    public Collection<ImenuFavorite> findFavoritesByDeviceId(String deviceId) {
        try {
            return (List<ImenuFavorite>) getEntityManager().createNamedQuery("ImenuFavorite.findFavoritesByDeviceId").setParameter("deviceId", deviceId).getResultList();
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<ImenuFavorite>();
        }
    }
    
    public void cancelFavorite(String deviceId, Integer menuId){
        
    }
    
    public void remove(Integer favoriteId){
        int updated = getEntityManager().createQuery("DELETE FROM ImenuFavorite f WHERE f.id = :favoriteId").setParameter("favoriteId", favoriteId).executeUpdate();
        System.out.println("ImenuFavoriteFacade.remove() favoriteId="+favoriteId+" updated="+updated);
    }
}
